//
//  TeachManagementController.m
//  Coach_HaiGeEr
//
//  Created by liuchen on 2018/9/26.
//  Copyright © 2018年 海格尔. All rights reserved.
//

#import "TeachManagementController.h"
#import "TeachManagementSegmentedView.h"
#import "TeachManagementChildController.h"
#import "ExaminaColumnView.h"

@interface TeachManagementController ()
@property (nonatomic,assign) BOOL segmentViewHidenJudge;
@property (nonatomic,strong) TeachManagementSegmentedView *segmentView;
@property (nonatomic,strong) ExaminaColumnView *indicatorView;
@property (nonatomic,strong) TeachManagementChildController *subjectTwo;
@property (nonatomic,strong) TeachManagementChildController *subjectThree;
@property (nonatomic,assign) NSUInteger senderNum;
@property (nonatomic,copy) NSArray *subjectTwoArr;
@property (nonatomic,copy) NSArray *subjectTwoSourceArr;
@end

@implementation TeachManagementController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [[CoachTabBarController shareLZHTabbarController].tabBar setHidden:YES];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"学员信息";
    [self setNavBack];
    [self returnCoachType];
    [self getSubjectTwoProject];
}

#pragma mark ==========获取科目二考试项目==========
- (void)getSubjectTwoProject{
    if ([UserMessage sharedInstance].model.zjkmNum==1 || [UserMessage sharedInstance].model.zjkmNum==2) {
        NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
        [dic setValue:[NSNumber numberWithInteger:2] forKey:@"subject"];
        [dic setValue:[UserMessage sharedInstance].model.currentAdCode forKey:@"area"];
        [dic setValue:@"C1" forKey:@"cartype"];
        [[NetworkEngine sharedClient] GETByToken:TEACHMANAGEMENTSUBJECTTWOPROJECT data:dic success:^(id responseObject) {
            if ([[responseObject objectForKey:@"code"] integerValue] == 1) {
                self.subjectTwoArr = [self analysisSubjectTwoWithArr:[responseObject objectForKey:@"data"]];
                self.subjectTwoSourceArr = [responseObject objectForKey:@"data"];
                [self createSubView];
            }else if ([[responseObject objectForKey:@"code"] integerValue] == 2) {
                [self toLoginVC];
            }
        } failure:^(NSString *error) {
            [self createSubView];
        }];
    }else {
        [self createSubView];
    }
}

#pragma mark ==========创建视图==========
- (void)createSubView{
    self.view.backgroundColor = [UIColor whiteColor];
    if ([UserMessage sharedInstance].model.zjkmNum==1) {
        [self.view addSubview:self.segmentView];
        TeachManagementChildController *subjectTwo = [[TeachManagementChildController alloc] init];
        subjectTwo.subjectTwoArr = self.subjectTwoArr;
        subjectTwo.subjectTwoSourceArr = self.subjectTwoSourceArr;
        subjectTwo.num = 2;
        subjectTwo.frameJudge = YES;
        subjectTwo.view.frame = CGRectMake(0, CGRectGetMaxY(self.segmentView.frame), self.view.frame.size.width, self.view.frame.size.height-CGRectGetMaxY(self.segmentView.frame));
        [subjectTwo.view setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
        _subjectTwo = subjectTwo;
        [self addChildViewController:self.subjectTwo];
        [self.view addSubview:self.subjectTwo.view];
        TeachManagementChildController *subjectThree = [[TeachManagementChildController alloc] init];
        subjectThree.num = 3;
        subjectThree.frameJudge = YES;
        subjectThree.view.frame = CGRectMake(SCREEN_WIDTH, CGRectGetMaxY(self.segmentView.frame), self.view.frame.size.width, self.view.frame.size.height-CGRectGetMaxY(self.segmentView.frame));
        [subjectThree.view setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
        _subjectThree = subjectThree;
        [self addChildViewController:self.subjectThree];
        [self.view addSubview:self.subjectThree.view];
    } else if ([UserMessage sharedInstance].model.zjkmNum==2) {
        TeachManagementChildController *subjectTwo = [[TeachManagementChildController alloc] init];
        subjectTwo.subjectTwoArr = self.subjectTwoArr;
        subjectTwo.subjectTwoSourceArr = self.subjectTwoSourceArr;
        subjectTwo.num = 2;
        subjectTwo.view.frame = CGRectMake(0, SYSTEM_TOP, SCREEN_WIDTH, SCREEN_HEIGHT-SYSTEM_TOP);
        [self addChildViewController:subjectTwo];
        [self.view addSubview:subjectTwo.view];
    } else if ([UserMessage sharedInstance].model.zjkmNum==3) {
        TeachManagementChildController *subjectThree = [[TeachManagementChildController alloc] init];
        subjectThree.num = 3;
        subjectThree.view.frame = CGRectMake(0, SYSTEM_TOP, SCREEN_WIDTH, SCREEN_HEIGHT-SYSTEM_TOP);
        [self addChildViewController:subjectThree];
        [self.view addSubview:subjectThree.view];
    }
//    [self.view addSubview:self.indicatorView];
//    [self addChildViewController:self.subjectTwo];
//    [self addChildViewController:self.subjectThree];
}

#pragma mark ==========懒加载==========
- (ExaminaColumnView *)indicatorView{
    if (!_indicatorView) {
        _indicatorView = [[ExaminaColumnView alloc] initWithFrame:CGRectMake(0, SYSTEM_TOP, SCREEN_WIDTH, SCREEN_HEIGHT-SYSTEM_TOP) WithTitle:@[@"科目二",@"科目三"]];
        [_indicatorView.contentSrcView addSubview:self.subjectTwo.view];
        [_indicatorView.contentSrcView addSubview:self.subjectThree.view];
    }
    return _indicatorView;
}
- (TeachManagementChildController *)subjectTwo{
    if (!_subjectTwo) {
        _subjectTwo = [[TeachManagementChildController alloc] init];
        _subjectTwo.subjectTwoArr = self.subjectTwoArr;
        _subjectTwo.subjectTwoSourceArr = self.subjectTwoSourceArr;
        _subjectTwo.num = 2;
        _subjectTwo.view.frame = CGRectMake(0, CGRectGetMaxY(self.segmentView.frame), self.view.frame.size.width, self.view.frame.size.height-CGRectGetMaxY(self.segmentView.frame));
    }
    return _subjectTwo;
}
- (TeachManagementChildController *)subjectThree{
    if (!_subjectThree) {
        _subjectThree = [[TeachManagementChildController alloc] init];
        _subjectThree.num = 3;
        _subjectThree.view.frame = CGRectMake(SCREEN_WIDTH, CGRectGetMaxY(self.segmentView.frame), self.view.frame.size.width, self.view.frame.size.height-CGRectGetMaxY(self.segmentView.frame));
    }
    return _subjectThree;
}

- (TeachManagementSegmentedView *)segmentView{
    if (!_segmentView) {
        _segmentView = [[TeachManagementSegmentedView alloc] initWithFrame:CGRectMake(0, SYSTEM_TOP, SCREEN_WIDTH, 120*SCALEUNIT) WithTitleArray:[NSArray arrayWithObjects:@"科二",@"科三", nil]];
        [_segmentView.segment addTarget:self action:@selector(changeView:) forControlEvents:UIControlEventValueChanged];
    }
    return _segmentView;
}

#pragma mark ==========切换视图==========
- (void)changeView:(UISegmentedControl *)sender{
    NSUInteger index = [sender selectedSegmentIndex];
    if (index==0 && index!=self.senderNum) {
        self.senderNum = index;
        [UIView animateWithDuration:0.5f animations:^{
            self.subjectTwo.view.center = CGPointMake(self.view.frame.size.width*0.5, SCREEN_HEIGHT*0.5+CGRectGetMaxY(self.segmentView.frame)*0.5);
            self.subjectThree.view.center = CGPointMake(self.view.frame.size.width*1.5, SCREEN_HEIGHT*0.5+CGRectGetMaxY(self.segmentView.frame)*0.5);
        }];
    }else if (index==1 && index!=self.senderNum) {
        self.senderNum = index;
        [UIView animateWithDuration:0.5f animations:^{
            self.subjectTwo.view.center = CGPointMake(-self.view.frame.size.width*0.5, SCREEN_HEIGHT*0.5+CGRectGetMaxY(self.segmentView.frame)*0.5);
            self.subjectThree.view.center = CGPointMake(self.view.frame.size.width*0.5, SCREEN_HEIGHT*0.5+CGRectGetMaxY(self.segmentView.frame)*0.5);
        }];
    }else{}
}

#pragma mark ==========科目二类型数组解析==========
- (NSArray *)analysisSubjectTwoWithArr:(NSArray *)array{
    NSMutableArray *returnArr = [[NSMutableArray alloc] init];
    if (array.count!=0) {
        [returnArr addObject:@"全部项目"];
        for (NSDictionary *dic in array) {
            [returnArr addObject:[dic objectForKey:@"itemName"]];
        }
    }
    return returnArr;
}

#pragma mark ==========验证科目类型==========
- (void)returnCoachType{//1全部 2科目二 3科目三 4科目四 MineBoyImage
    NSInteger solution=0;
    if ([[UserMessage sharedInstance].model.zjkm containsString:@"2"] && [[UserMessage sharedInstance].model.zjkm containsString:@"3"]) {
        solution=1;
        [UserMessage sharedInstance].model.zjkmNum = solution;
        return;
    }else if ([[UserMessage sharedInstance].model.zjkm containsString:@"2"]) {
        solution=2;
        [UserMessage sharedInstance].model.zjkmNum = solution;
        return;
    }else if ([[UserMessage sharedInstance].model.zjkm containsString:@"3"]) {
        solution=3;
        [UserMessage sharedInstance].model.zjkmNum = solution;
        return;
    }else if ([[UserMessage sharedInstance].model.zjkm containsString:@"4"]) {
        solution=4;
        [UserMessage sharedInstance].model.zjkmNum = solution;
        return;
    }else {}
}

- (void)fitFrameForChildViewController:(UIViewController *)chileViewController{
    CGRect frame = self.view.frame;
    frame.origin.y = 0;
    chileViewController.view.frame = frame;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
